package com.bt.dmdb.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bt.dmdb.dao.QuestionDao;
import com.bt.dmdb.model.CustomerDetails;
import com.bt.dmdb.model.CustomerQuestion;
import com.bt.dmdb.model.Question;
import com.bt.dmdb.model.QuestionDTO;
import com.bt.dmdb.model.QuestionRDTO;
@Repository("questionDao")
public class QuestionDaoImpl extends GenericDaoImpl<Question, Long> implements QuestionDao {

	public QuestionDaoImpl() {
		super(Question.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Question> fetchFfeQuestions() {
    	List<Question> listQuestion = (List<Question>) cs().createCriteria(Question.class).add(Restrictions.eq("category", "FFI_Common")).list();
		return listQuestion;
	}
	@SuppressWarnings("unchecked")
	@Override
	public String saveSpecificFFIAnswers(QuestionDTO y){
		List<Long> listids = (List<Long>) cs().createCriteria(Question.class)
    			.add(Restrictions.and(Restrictions.ne("classification", "MAKER")
    			,Restrictions.ne("category", "NFFE_Common")
    			,Restrictions.ne("category", "FFI_Common")
    			))
    			.setProjection(Projections.projectionList()
    					.add(Projections.property("id"))).list();
	    					
		 List<CustomerQuestion> customerQuestions = (List<CustomerQuestion>) cs().createCriteria(CustomerQuestion.class)
	    			.add(Restrictions.and(Restrictions.eq("customer.id", new Long(y.getId())),   Restrictions.in("question.id",  listids)))
	    			.list();
		 if(customerQuestions != null && customerQuestions.size()>0){
			 for(CustomerQuestion customerQuestion : customerQuestions){
				 cs().delete(customerQuestion);
			 } 
		 }
		CustomerDetails customerDetails = findCustomerById(new Long(y.getId()));
		customerDetails.setStatus(CHECKER_REVIEW_PENDING);
		 StringBuffer auditLog = new StringBuffer(customerDetails.getAuditLog());
   	     if(!customerDetails.getAuditLog().contains(CHECKER_REVIEW_PENDING))
         auditLog.append(":"+CHECKER_REVIEW_PENDING);
   	     customerDetails.setAuditLog(auditLog.toString());
		customerDetails.setFatcaStatus(y.getFatcaStatusSpe());
		cs().update(customerDetails);
		for(Map<String, String> m: y.getName()){
			Question question = null;
			String answer = null;
	    	for(Map.Entry<String, String> entry: m.entrySet()) {
	    	    if(entry.getKey() == "q")     	    	
	    	    	question = findQuestionById(new Long(entry.getValue()));
	    	    if(entry.getKey() == "a")     	    	
	    	    	answer = entry.getValue();	
	    	}
	    	char ans='\u0000';
	        if(answer.equals("Yes"))ans='Y';
	        if(answer.equals("No"))ans='N';
	        if(answer.equals("Don't Know"))ans='D';
	    	 saveResponse(customerDetails, question, ans, "");
	    	
	    	}
		return null;
	}
	
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public String saveSpecificNFFEAnswers(QuestionDTO y){
			List<Long> listids = (List<Long>) cs().createCriteria(Question.class)
	    			.add(Restrictions.and(Restrictions.ne("classification", "MAKER")
	    			,Restrictions.ne("category", "NFFE_Common")
	    			,Restrictions.ne("category", "FFI_Common")
	    			))
	    			.setProjection(Projections.projectionList()
	    					.add(Projections.property("id"))).list();
	    					
		 List<CustomerQuestion> customerQuestions = (List<CustomerQuestion>) cs().createCriteria(CustomerQuestion.class)
	    			.add(Restrictions.and(Restrictions.eq("customer.id", new Long(y.getId())),   Restrictions.in("question.id",  listids)))
	    			.list();
		 if(customerQuestions != null && customerQuestions.size()>0){
			 for(CustomerQuestion customerQuestion : customerQuestions){
				 cs().delete(customerQuestion);
			 } 
		 }
		CustomerDetails customerDetails = findCustomerById(new Long(y.getId()));
		customerDetails.setStatus(CHECKER_REVIEW_PENDING);
		 StringBuffer auditLog = new StringBuffer(customerDetails.getAuditLog());
    	 if(!customerDetails.getAuditLog().contains(CHECKER_REVIEW_PENDING))
         auditLog.append(":"+CHECKER_REVIEW_PENDING);
    	 customerDetails.setAuditLog(auditLog.toString());
		customerDetails.setFatcaStatus(y.getFatcaStatusSpe());
		cs().update(customerDetails);
		for(Map<String, String> m: y.getName()){
			Question question = null;
			String answer = null;
	    	for(Map.Entry<String, String> entry: m.entrySet()) {
	    	    if(entry.getKey() == "q")     	    	
	    	    	question = findQuestionById(new Long(entry.getValue()));
	    	    if(entry.getKey() == "a")     	    	
	    	    	answer = entry.getValue();	
	    	}

		     char ans='\u0000';
		     if(answer.equals("Yes"))ans='Y';
		     if(answer.equals("No"))ans='N';
		     if(answer.equals("Don't Know"))ans='D';
	    	 saveResponse(customerDetails, question, ans, "");
	    	
	    	}
		return null;
	}
	
	
	@Override
	public String saveMakerFFIAnswers(String custID, String quesId0,
			String ansId0, String quesId1, String ansId1, String explanation,
			 String quesId2, String ansId2, String fatcaStatusFFI) {
		
				List<Long> listids = (List<Long>) cs().createCriteria(Question.class)
		    			.add(Restrictions.and(Restrictions.ne("classification", "MAKER")
		    			,Restrictions.or(Restrictions.eq("category", "NFFE_Common")
		    			,Restrictions.eq("category", "FFI_Common"))
		    			))
		    			.setProjection(Projections.projectionList()
		    					.add(Projections.property("id"))).list();
		    					
			 List<CustomerQuestion> customerQuestions = (List<CustomerQuestion>) cs().createCriteria(CustomerQuestion.class)
		    			.add(Restrictions.and(Restrictions.eq("customer.id", new Long(custID)),   Restrictions.in("question.id",  listids)))
		    			.list();
			 if(customerQuestions != null && customerQuestions.size()>0){
				 for(CustomerQuestion customerQuestion : customerQuestions){
					 cs().delete(customerQuestion);
				 } 
			 }
	       CustomerDetails customerDetails = findCustomerById(new Long(custID));
	       customerDetails.setFatcaStatus(fatcaStatusFFI);
	       cs().update(customerDetails);
	       Question question0 = findQuestionById(new Long(quesId0));
	       Question question1 = findQuestionById(new Long(quesId1));
	       Question question2 = findQuestionById(new Long(quesId2));
	       char ans0='\u0000';
	       if(ansId0.equals("Yes"))ans0='Y';
	       if(ansId0.equals("No"))ans0='N';
	       if(ansId0.equals("Don't Know"))ans0='D';
	       
	       char ans1='\u0000';
	       if(ansId1.equals("Yes"))ans1='Y';
	       if(ansId1.equals("No"))ans1='N';
	       if(ansId1.equals("Don't Know"))ans1='D';
	       
	       char ans2='\u0000';
	       if(ansId2.equals("Yes"))ans2='Y';
	       if(ansId2.equals("No"))ans2='N';
	       if(ansId2.equals("Don't Know"))ans2='D';
	       
	       if(ansId0.equals("No"))	{
	    	   explanation = "explanation : "+ explanation;
	    	   saveResponse(customerDetails, question0, ans0, explanation);
	       }else
	    	   saveResponse(customerDetails, question0, ans0, "");
	       saveResponse(customerDetails, question1, ans1, "");  
	       saveResponse(customerDetails, question2, ans2, "");
	      
	       return question0.getClassification();
	}
	
	@Override
	public String saveMakerNFFEAnswers(String custID,String quesId0,String ansId0,
    		String quesId1,String ansId1,
      		String explanation,
      		String quesId2,String ansId2, 
      		String quesId3,String ansId3, 
      		String quesId4,String ansId4, 
      		String quesId5,String ansId5, 
      		String quesId6,String ansId6,String fatcaStatus) {
		
				List<Long> listids = (List<Long>) cs().createCriteria(Question.class)
		    			.add(Restrictions.and(Restrictions.ne("classification", "MAKER")
		    			,Restrictions.or(Restrictions.eq("category", "NFFE_Common")
		    			,Restrictions.eq("category", "FFI_Common"))
		    			))
		    			.setProjection(Projections.projectionList()
		    					.add(Projections.property("id"))).list();
		    					
			 List<CustomerQuestion> customerQuestions = (List<CustomerQuestion>) cs().createCriteria(CustomerQuestion.class)
		    			.add(Restrictions.and(Restrictions.eq("customer.id", new Long(custID)),   Restrictions.in("question.id",  listids)))
		    			.list();
			 if(customerQuestions != null && customerQuestions.size()>0){
				 for(CustomerQuestion customerQuestion : customerQuestions){
					 cs().delete(customerQuestion);
				 } 
			 }
	 
	       CustomerDetails customerDetails = findCustomerById(new Long(custID));
	       customerDetails.setFatcaStatus(fatcaStatus);
	       cs().update(customerDetails);
	       Question question0 = findQuestionById(new Long(quesId0));
	       Question question1 = findQuestionById(new Long(quesId1));
	       Question question2 = findQuestionById(new Long(quesId2));
	       Question question3 = findQuestionById(new Long(quesId3));
	       Question question4 = findQuestionById(new Long(quesId4));
	       Question question5 = findQuestionById(new Long(quesId5));
	       Question question6 = findQuestionById(new Long(quesId6));
	       
	       char ans0='\u0000';
	       if(ansId0.equals("Yes"))ans0='Y';
	       if(ansId0.equals("No"))ans0='N';
	       if(ansId0.equals("Don't Know"))ans0='D';
	       
	       char ans1='\u0000';
	       if(ansId1.equals("Yes"))ans1='Y';
	       if(ansId1.equals("No"))ans1='N';
	       if(ansId1.equals("Don't Know"))ans1='D';
	       
	       char ans2='\u0000';
	       if(ansId2.equals("Yes"))ans2='Y';
	       if(ansId2.equals("No"))ans2='N';
	       if(ansId2.equals("Don't Know"))ans2='D';
	       
	       char ans3='\u0000';
	       if(ansId3.equals("Yes"))ans3='Y';
	       if(ansId3.equals("No"))ans3='N';
	       if(ansId3.equals("Don't Know"))ans3='D';
	       
	       char ans4='\u0000';
	       if(ansId4.equals("Yes"))ans4='Y';
	       if(ansId4.equals("No"))ans4='N';
	       if(ansId4.equals("Don't Know"))ans4='D';
	       
	       char ans5='\u0000';
	       if(ansId5.equals("Yes"))ans5='Y';
	       if(ansId5.equals("No"))ans5='N';
	       if(ansId5.equals("Don't Know"))ans5='D';
	       
	       char ans6='\u0000';
	       if(ansId6.equals("Yes"))ans6='Y';
	       if(ansId6.equals("No"))ans6='N';
	       if(ansId6.equals("Don't Know"))ans6='D';
	       
	       if(ansId0.equals("No"))	{
	    	   explanation = "explanation : "+ explanation;
	    	   saveResponse(customerDetails, question0, ans0, explanation);
	       }else
	    	   saveResponse(customerDetails, question0, ans0, "");
	       saveResponse(customerDetails, question1, ans1, "");  
	       saveResponse(customerDetails, question2, ans2, "");
	       saveResponse(customerDetails, question3, ans3, "");  
	       saveResponse(customerDetails, question4, ans4, "");
	       saveResponse(customerDetails, question5, ans5, "");  
	       saveResponse(customerDetails, question6, ans6, "");
	      
	       return question0.getClassification();
	}
	
	 public Question findQuestionById(long id) {
	        return (Question) cs().createCriteria(Question.class).add(Restrictions.eq("id", id)).uniqueResult();
	    }
	 
	 @Override
	    public CustomerDetails findCustomerById(long id) {
	        return (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", id)).uniqueResult();
	    }
	 
	 public void saveResponse(CustomerDetails customerDetails, Question question, char answer, String comment) {
	    	CustomerQuestion customerQuestion = (CustomerQuestion) cs().createCriteria(CustomerQuestion.class)
	    			.add(Restrictions.and(Restrictions.eq("customer.id", customerDetails.getId()), Restrictions.eq("question.id", question.getId())))
	    			.uniqueResult();
	    	if(customerQuestion != null){
	    		customerQuestion.setCustomer(customerDetails);
	        	customerQuestion.setQuestion(question);
	        	customerQuestion.setAnswer(answer);
	        	customerQuestion.setComment(comment);        	
	            cs().update(customerQuestion);
	    	}else{
	    		CustomerQuestion customerQues = new CustomerQuestion();
	    		customerQues.setCustomer(customerDetails);
	    		customerQues.setQuestion(question);
	    		customerQues.setAnswer(answer);
	    		customerQues.setComment(comment);
	        	
	            cs().save(customerQues);
	    	}
	    	
	    }
	 
	 @SuppressWarnings("unchecked")
	@Override
	 public String findClassification(Long id){
		 List<CustomerQuestion> customerQuestions = (List<CustomerQuestion>)cs().createCriteria(CustomerQuestion.class)
				 .createAlias("question", "q").add(Restrictions.eq("customer.id", id)).list();
		 String classification = "";
		 if(customerQuestions != null) {
			 CustomerQuestion customerQuestion = customerQuestions.get(customerQuestions.size()-1);
			 classification = customerQuestion.getQuestion().getClassification();
		 }
		 return classification ;
	 }


	@Override
	public String saveCheckerFinalStatus(QuestionRDTO y){
		//if(y.getStatus().contains("Approved")){
			CustomerDetails customerDetails = findCustomerById(new Long(y.getId()));
			
			customerDetails.setStatus(y.getStatus());
			StringBuffer auditLog = new StringBuffer(customerDetails.getAuditLog());
	   	    if(!customerDetails.getAuditLog().contains(y.getStatus()))
	        auditLog.append(":"+y.getStatus());
	   	    customerDetails.setAuditLog(auditLog.toString());
					
			customerDetails.setMoreInfo(y.getMoreInfo());
			customerDetails.setIsInfoMissing(y.getIsInfoMissing());
			customerDetails.setIsDocMissing(y.getIsDocMissing());
			cs().update(customerDetails);
		/*}
		if(y.getStatus().contains("Rejected")){
			CustomerDetails customerDetails = findCustomerById(new Long(y.getId()));
			
			customerDetails.setStatus(CHECKER_APPROVED_PENDING);
			StringBuffer auditLog = new StringBuffer(customerDetails.getAuditLog());
	   	    if(!customerDetails.getAuditLog().contains(CHECKER_APPROVED_PENDING))
	        auditLog.append(":"+CHECKER_APPROVED_PENDING);
	   	    customerDetails.setAuditLog(auditLog.toString());
			
			customerDetails.setStatus(y.getStatus());
			customerDetails.setMoreInfo(y.getMoreInfo());
			customerDetails.setIsInfoMissing(y.getIsInfoMissing());
			customerDetails.setIsDocMissing(y.getIsDocMissing());
			cs().update(customerDetails);
		}
		if(y.getStatus().contains("Reinitated")){
			CustomerDetails customerDetails = findCustomerById(new Long(y.getId()));
			
			customerDetails.setStatus(CHECKER_APPROVED_PENDING);
			StringBuffer auditLog = new StringBuffer(customerDetails.getAuditLog());
	   	    if(!customerDetails.getAuditLog().contains(CHECKER_APPROVED_PENDING))
	        auditLog.append(":"+CHECKER_APPROVED_PENDING);
	   	    customerDetails.setAuditLog(auditLog.toString());
					
			customerDetails.setStatus(y.getStatus());
			customerDetails.setMoreInfo(y.getMoreInfo());
			customerDetails.setIsInfoMissing(y.getIsInfoMissing());
			customerDetails.setIsDocMissing(y.getIsDocMissing());
			cs().update(customerDetails);
		}*/
		
		return null;
	}

	
}
